// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: Implementation of CDefaultUIEventHandlerMap.
//   $Id$

#include "stdafx.h"
#include "defaultuieventhandlermap.h"
#include "xformsElement.h"
#include "modelitemPropertiesUI.h"
#include "defaultUIEvents.h"
#include "events.h"
HRESULT CDefaultUIEventHandlerMap::GetHandlingFunction(const BSTR sEventName,HandlerFunction * ppFunc)
{
	HRESULT hr = S_OK;

	if(!sm_hFuncMapMutex)
		sm_hFuncMapMutex = CreateMutex(0, FALSE, _T("Local\\DefaultUIEventHandlerFuncMapMutex"));

	WaitForSingleObject(sm_hFuncMapMutex, INFINITE);

	try
	{
		//If this is the first time this function has been called,
		//	fill the map to allow lookup.
		if(sm_FuncMap.empty())
		{
			createHandlerMap();
		}

		//Fetch the function pointer out of the map
		CMapBSTRToFunc::const_iterator iterator;
		iterator = sm_FuncMap.find(sEventName);
		if(iterator == sm_FuncMap.end())
			*ppFunc = 0;
		else
			*ppFunc = iterator->second;
	}
	catch(...)
	{
	}

	ReleaseMutex(sm_hFuncMapMutex);

	return hr;
}

void CDefaultUIEventHandlerMap::createHandlerMap()
{
	sm_FuncMap.insert(strFuncPair(EVENT_READONLY,&readonly));
	sm_FuncMap.insert(strFuncPair(EVENT_READWRITE,&readwrite));
	sm_FuncMap.insert(strFuncPair(EVENT_REQUIRED,&required));
	sm_FuncMap.insert(strFuncPair(EVENT_OPTIONAL,&optional));
	sm_FuncMap.insert(strFuncPair(EVENT_VALID,&valid));
	sm_FuncMap.insert(strFuncPair(EVENT_INVALID,&invalid));
	sm_FuncMap.insert(strFuncPair(EVENT_ENABLED,&enabled));
	sm_FuncMap.insert(strFuncPair(EVENT_DISABLED,&disabled));
	sm_FuncMap.insert(strFuncPair(EVENT_ACTIVATE,&activate));
	sm_FuncMap.insert(strFuncPair(EVENT_VALUECHANGED,&valuechanged));
	sm_FuncMap.insert(strFuncPair(EVENT_OUTOFRANGE,&outofrange));
	sm_FuncMap.insert(strFuncPair(EVENT_INRANGE,&inrange));
}

CMapBSTRToFunc CDefaultUIEventHandlerMap::sm_FuncMap;
HANDLE CDefaultUIEventHandlerMap::sm_hFuncMapMutex = 0;
